<?
	layout("/inc/layout.html",{title:sysTitle}){
?>

<style type="text/css">
	
	.aren_count_input:focus{
		background: #52a032;
		border: 0;
		border-radius: 5px;
		box-shadow: 0px 0px 3px #000;
		color:#fff;
	}
	.aren_count_input input .on{
		background: #52a032;
		border: 0;
		border-radius: 5px;
		box-shadow: 0px 0px 3px #000;
		color:#fff;
	}
	.aren_count_input.on{
		background: #52a032;
		border: 0;
		border-radius: 5px;
		box-shadow: 0px 0px 3px #000;
		color:#fff;
	}
</style>
<div id="page-wrapper">
	<!--BEGIN TITLE & BREADCRUMB PAGE-->
	<div id="title-breadcrumb-option-demo" class="page-title-breadcrumb">
		<div class="page-header pull-left">
			<div class="page-title">分库分表设置</div>
		</div>
		<ol class="breadcrumb page-breadcrumb pull-left">
			<li><i class="fa fa-home"></i>&nbsp;<a href="main.html">首页</a>&nbsp;&nbsp;<i
				class="fa fa-angle-right"></i>&nbsp;&nbsp;</li>
			<li>系统管理&nbsp;&nbsp;<i class="fa fa-angle-right"></i>&nbsp;&nbsp;</li>
			<li class="active">分库分表设置</li>
		</ol>

		<div class="clearfix"></div>
	</div>
	<!--END TITLE & BREADCRUMB PAGE-->
	<!--BEGIN CONTENT-->
	<div class="page-content">

		<!-- 管理界面 start -->
		<div class="row">
			<div class="col-lg-12">
				<div class="note note-danger">
					<h3 class="box-heading">Tips</h3>
					<p>
		           	1、分库分表设置是大数据量和高并发项目性能优化的策略之一，目前支持分库分表；需要配置数据源信息和分库分表策略；
		            </p>
		            <p>
		           	2、数据源配置目前支持MYSQL数据库，如不同机器之间的读写分离功能，需配置数据库数据同步的功能；
		            </p>
		            <p>
		           	3、分库分表和数据源配置功能需在建立知识体系结构之前构建完毕，禁止在建立知识结构后再调整，出现问题不予以维护和处理；
		            </p>
		            <p>
		           	4、数据源配置和分库分表策略变更后都需要进行数据发布操作。
		            </p>
				</div>
			</div>
		</div>
		<div class="row mbl">
           <div class="col-lg-6">
				<h4># 数据源配置 <button class="btn btn btn-grey " onclick="toShardingRepositoryDataSource('0')">+</button></h4>
			</div>
			<div class="col-lg-6">
				<div class="tb-group-actions pull-right">
           			<button class="btn btn btn-success" onclick="toDatabase()">数据源管理</button>
               </div>
			</div>
		</div>
		<div class="row" style="margin-bottom: 10px;">
			<div class="col-lg-12">
				<span style="font-size: 20px;">数据源：<button class="btn btn-sm btn-default aren_count_input on" onclick="getShardingRepositoryDataSource('0');">全部</button></span>
				<span style="font-size: 20px;" id="databaseText">
				</span>
			</div>
		</div>
		<div class="row" id="shardingRepositoryDataSourceText">
            
            
        </div>
	</div>
	<!--END CONTENT-->
</div>


<?} ?>
<script type="text/javascript">
var databaseValue=0;
$(function() {
	getDatabaseAll();
	getShardingRepositoryDataSource('0');
	
	$('.page-content').on('click','.aren_count_input',function(){
		$('.aren_count_input').removeClass('on');
		$(this).addClass('on');
	})
});
function toDatabase() {
	layer.open({
		type : 2,
		area : [ '700px', '600px' ],
		scrollbar : false,
		shadeClose : true,
		fix : false, //不固定
		title : '数据源管理',
		//maxmin : true,  
		skin : 'layui-layer-rim', //加上边框
		btn : [ '关闭' ],
		content : [ "knowledge-edit-sharding-database-list.html", 'no' ],
		yes : function(index, layer) {
			var iframeWin = parent.window[layer.find('iframe')[0]['name']];
			iframeWin.DatabaseList();
		},
		cancel : function(index) {
			getDatabaseAll();
		}
	});
}
//查询全部数据源
function getDatabaseAll(){
	$.ajax({
		type : "post",
		url : "getDatabaseAll.do",
		dataType : "json",
		data:{},
		traditional : true,
		beforeSend : function(XMLHttpRequest) {},
		success:function(data){
			var src='';
			for (i=0;i<data.length ;i++ ){
				src+= '<button class="btn btn-sm btn-default aren_count_input" style="margin-right: 5px;" onclick="getShardingRepositoryDataSource('+data[i].id+')">'+data[i].nameZh+'（'+data[i].nameEn+'）</button>';
			}
			$("#databaseText").html(src);
		},
		error:function(){
			alert("编辑失败");
		}

	});
}
var shardingRepositoryDataSourceId;
function getShardingRepositoryDataSourceId() {
	return shardingRepositoryDataSourceId;
}
//配置数据源
function toShardingRepositoryDataSource(repositoryDataSourceId) {
	shardingRepositoryDataSourceId = repositoryDataSourceId
	layer.open({
		type : 2,
		area : [ '550px', '450px' ],
		scrollbar : false,
		shadeClose : true,
		fix : false, //不固定
		title : '配置数据源',
		//maxmin : true,  
		skin : 'layui-layer-rim', //加上边框
		btn : [ '保存','关闭' ],
		content : [ "sharding-repository-data-source.html", 'no' ],
		yes : function(index, layer) {
			var iframeWin = parent.window[layer.find('iframe')[0]['name']];
			iframeWin.Page();
		},
		cancel : function(index) {
			
		}
	});
}
//查询数据源下表
function getShardingRepositoryDataSource(dataSource){
	databaseValue = dataSource;
	$.ajax({
		type : "post",
		url : "getShardingRepositoryDataSource.do",
		dataType : "json",
		data:{
			"dataSource":dataSource
		},
		traditional : true,
		beforeSend : function(XMLHttpRequest) {},
		success:function(data){
			var src = '';
			for (i=0;i<data.length ;i++ ){
				src += '<div class="col-lg-4">'
	                +'<div class="panel">'
	                +'<div class="panel-heading clearfix">'
	                +'<span class="mts pull-left">'+data[i].calssesName+'</span>'
	                +'<div class="toolbars">'
	                +'<div class="input-icon right">';
	                if(data[i].statusType == '0' ){
	                	src +='<button class="btn btn btn-success" onclick="structureChart(\''+data[i].id+'\',\''+data[i].calssesNameEn+'\');">构建</button>&nbsp;&nbsp;&nbsp;';
		                src +='<button class="btn btn-sm btn-grey" onclick="toShardingRepositoryDataSource('+data[i].id+');">编辑</button>&nbsp;&nbsp;&nbsp;';
	                }
	                src +='<button class="btn btn-sm btn-danger" onclick="deleteShardingRepositoryDataSource(\''+data[i].id+'\',\''+data[i].calssesNameEn+'\');">删除</button>&nbsp;&nbsp;&nbsp;'
	                +'</div>'
	                +'</div>'
	                +'</div>'
	                +'<div class="panel-body">'
	                +'<p style="font-size: 15px;">'
	                +'是否开启分库分表：'+data[i].openSharding
	                +'</p>'
	                +'<p style="font-size: 15px;">'
	                +'单表记录阈值：'+data[i].shardingThreshold
	                +'</p>'
	                +'<p style="font-size: 15px;">'
	                +'数据源：<span class="badge badge-danger">'+data[i].shardingDataSourceName+'</span>'
	                +'</p>'
	                +'<p style="font-size: 15px;">'
	                +'状态：'+data[i].status
	                +'</p>'
	                +'<p style="font-size: 15px; test-overflow:ellipsis;">'
	                +'数据库表：<button type="button" onclick="queryChart(\''+data[i].dataSourceId+'\',\''+data[i].statusType+'\',\''+data[i].calssesNameEn+'\');" class="btn btn-sm btn-default">查看数据库表</button>'
	                +'</p>'
	                +'</div>'
	                +'</div>'
	                +'</div>';
			}
			$("#shardingRepositoryDataSourceText").html(src);
		},
		error:function(){
			
		}

	})
}
//构建
function structureChart(structureChartDataSourceId,classesNameEn){
	if (confirm("是否确认构建！！")){
		$.ajax({
			type : "post",
			url : "structureChart.do",
			dataType : "json",
			data:{
				"id":structureChartDataSourceId,
				"classesNameEn":classesNameEn,
			},
			traditional : true,
			beforeSend : function(XMLHttpRequest) {},
			success:function(data){
				if(data[0] == 'yes'){
					layer.msg('构建成功');
				}else{
					layer.msg('构建失败');
				}
				getShardingRepositoryDataSource(databaseValue);
			},
			error:function(){
				alert("编辑失败");
			}
	
		});
	}
}
//删除
function deleteShardingRepositoryDataSource(structureChartDataSourceId,classesNameEn){
	if (confirm("是否确认删除！！")){
		$.ajax({
			type : "post",
			url : "deleteShardingRepositoryDataSource.do",
			dataType : "json",
			data:{
				"id":structureChartDataSourceId,
				"classesNameEn":classesNameEn,
			},
			traditional : true,
			beforeSend : function(XMLHttpRequest) {},
			success:function(data){
				if(data[0] == 'yes'){
					layer.msg('删除成功');
				}else{
					layer.msg('删除失败');
				}
				getShardingRepositoryDataSource(databaseValue);
			},
			error:function(){
				alert("编辑失败");
			}
		});
	}
}
var structureChartId;
function getStructureChartId() {
	return structureChartId;
}
var structureChartId;
function getStructureChartId() {
	return structureChartId;
}
var structureChartClassesEn;
function getStructureChartClassesEn() {
	return structureChartClassesEn;
}
//查看数据库表
function queryChart(chartDataSourceId,shardingRepositoryDataSourceType,chartClassesEn){
	if(shardingRepositoryDataSourceType != '0'){
		structureChartId = chartDataSourceId;
		structureChartClassesEn = chartClassesEn;
		layer.open({
			type : 2,
			area : [ '800px', '600px' ],
			scrollbar : false,
			shadeClose : true,
			fix : false, //不固定
			title : '数据库表',
			//maxmin : true,  
			skin : 'layui-layer-rim', //加上边框
			btn : [ '关闭' ],
			content : [ "knowledge-edit-sharding-database-chart.html", 'no' ],
		});
	}else{
		layer.msg('为构建，请先构建');
	}
}
</script>